package com.app.teaware.stock;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Parcelable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.app.teaware.R;
import com.app.teaware.basis.BasisActivity;
import com.app.teaware.basis.Constants;
import com.app.teaware.bean.BaseListData;
import com.app.teaware.bean.BaseListDataListBean;
import com.app.teaware.bean.CraftListBean;
import com.app.teaware.bean.CraftListData;
import com.app.teaware.bean.GoodsListBean;
import com.app.teaware.bean.GoodsListData;
import com.app.teaware.bean.LoginBean;
import com.app.teaware.bean.StockListBean;
import com.app.teaware.bean.StockListData;
import com.app.teaware.goods.GoodsListActivity;
import com.app.teaware.util.HexUtil;
import com.app.teaware.util.UHFUtil;
import com.jaredrummler.materialspinner.MaterialSpinner;
import com.nlscan.android.uhf.TagInfo;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import my.http.MyHttpListener;
import my.http.MyHttpUtils;
import my.http.MyParams;

public class CheckWareActivity extends BasisActivity {


    @BindView(R.id.recyclerview)
    RecyclerView mRecyclerView;
    @BindView(R.id.edit_gno)
    EditText edit_gno;
    StockListBean listBean;

    MyAdapterList mAdapter;

    @BindView(R.id.spinner_craft)
    MaterialSpinner spinner_craft;
    CraftListBean craftListBean;
    CraftListData curCraft;

    @Override
    public void initViews() {
        super.initViews();
        setContentView(R.layout.check_ware_activity);
        setTitle("库位盘点");
        setTitleLeftButton(null);
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
    }

    private BroadcastReceiver mResultReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent)
        {
            String action = intent.getAction();
            if(!Constants.ACTION_UHF_RESULT_SEND.equals(action))
                return ;
            Parcelable[] tagInfos =  intent.getParcelableArrayExtra(Constants.EXTRA_TAG_INFO);
            edit_gno.setText(UHFUtil.getEPCId(tagInfos).trim());
            getList();
        }//end onReceiver
    };

    @Override
    public void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        listBean = new StockListBean();
        initSpiner();
    }

    private void initSpiner(){
        showLoading();
        MyParams params = new MyParams();
        params.put("aid", LoginBean.getInstance().getAcode());
        MyHttpUtils.get(Constants.URL_CRAFT_SELECT_ALL, params, new MyHttpListener() {
                    @Override
                    public void onSuccess(int httpWhat, Object result) {
                        if(isFinishing()) return;
                        craftListBean = (CraftListBean) result;
                        if(craftListBean.getAllNames()!=null){
                            curCraft = craftListBean.getList().get(0);
                            spinner_craft.setItems(craftListBean.getAllNameList());
                            spinner_craft.setOnItemSelectedListener(new MaterialSpinner.OnItemSelectedListener<String>() {
                                @Override public void onItemSelected(MaterialSpinner view, int position, long id, String item) {
                                    curCraft = craftListBean.getList().get(position);
                                    getList();
                                }
                            });
                        }
                    }

                    @Override
                    public void onFailure(int httpWhat, Object result) {
                        super.onFailure(httpWhat, result);
                        hideLoading();
                    }

                    @Override
                    public void onFinish(int httpWhat) {
                        hideLoading();
                    }
                },
                0, CraftListBean.class);
    }

    private boolean httpIsFinish = true;
    private void getList(){
        if(TextUtils.isEmpty(edit_gno.getText().toString())){
            showToast("请先获取货架码");
            return;
        }
        if(!httpIsFinish) return;
        httpIsFinish = false;
        showLoading();
        MyParams params = new MyParams();
        params.put("aid",LoginBean.getInstance().getAcode());
        params.put("cid",curCraft.getCid());
        params.put("loc",edit_gno.getText().toString());

        MyHttpUtils.get(Constants.URL_STOCK_GET_DATA_BY_LOC, params, new MyHttpListener() {
                    @Override
                    public void onSuccess(int httpWhat, Object result) {
                        if (isFinishing()) return;
                        listBean = (StockListBean) result;
                        setListView();
                    }

                    @Override
                    public void onFailure(int httpWhat, Object result) {
                        super.onFailure(httpWhat, result);
                        dismissProgress();
                        setListView();
                    }

                    @Override
                    public void onFinish(int httpWhat) {
                        httpIsFinish = true;
                        dismissProgress();
                    }
                },
                0, StockListBean.class);
    }

    private void setListView() {
        if (mAdapter == null) {
            mAdapter = new MyAdapterList();
            mRecyclerView.setAdapter(mAdapter);
        } else {
            mAdapter.notifyDataSetChanged();
        }
    }

    public class MyAdapterList extends RecyclerView.Adapter<MyAdapterList.ViewHolder> {


        public MyAdapterList() {

        }

        //创建新View，被LayoutManager所调用
        @Override
        public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.check_ware_list_item, viewGroup, false);
            return new ViewHolder(view);
        }

        //将数据与界面进行绑定的操作
        @Override
        public void onBindViewHolder(ViewHolder viewHolder, int position) {
            viewHolder.bind(position);
        }

        //获取数据的数量
        @Override
        public int getItemCount() {
            int count = listBean == null ? 0 : listBean.getList().size();
            return count;
        }

        //自定义的ViewHolder，持有每个Item的的所有界面元素
        public class ViewHolder extends RecyclerView.ViewHolder {
            @BindView(R.id.text_code)
            public TextView text_code;
            @BindView(R.id.text_name)
            public TextView text_name;
            @BindView(R.id.text_specs)
            public TextView text_specs;
            @BindView(R.id.text_unit)
            public TextView text_unit;
            @BindView(R.id.text_num)
            public TextView text_num;
            @BindView(R.id.text_weight)
            public TextView text_weight;
            @BindView(R.id.text_kw)
            public TextView text_kw;
            @BindView(R.id.text_qrcode)
            public TextView text_qrcode;

            public ViewHolder(View view) {
                super(view);
                ButterKnife.bind(this, view);
            }

            public void bind(int position) {
                StockListData data = listBean.getList().get(position);
                text_code.setText("" + data.getGno());
                text_name.setText("" + data.getGna());
                text_specs.setText("" + data.getGsp());
                text_unit.setText("" + data.getGun());
                text_num.setText("" + data.getNum());
                text_weight.setText("" + data.getWgt());
                text_kw.setText("" + data.getLoc1());
                text_qrcode.setText("" + data.getRfid());
            }
        }
    }


    @Override
    protected void onPause() {
        super.onPause();
        unRegisterResultReceiver();
    }


    @Override
    protected void onResume() {
        super.onResume();
        registerResultReceiver();
    }

    private void registerResultReceiver() {
        try {
            IntentFilter iFilter = new IntentFilter(Constants.ACTION_UHF_RESULT_SEND);
            mContext.registerReceiver(mResultReceiver, iFilter);
        } catch (Exception e) {
        }

    }

    private void unRegisterResultReceiver() {
        try {
            mContext.unregisterReceiver(mResultReceiver);
        } catch (Exception e) {
        }

    }
}
